<?php if(!defined('BASEPATH')) exit ('No direct script access allowed');
class Add_person extends public_controller
{
	public function __construct() {
		parent:: __construct();
		if(!$this->session->userdata('loginStatus')) redirect('landing');
	}

	public function index() {

		//echo $this->util->calculator_age('1983-03-24', '2012-05-24');
		if($_POST) {
			$this->form_validation->set_rules('name', 'Name', 'required');
			$this->form_validation->set_rules('day_birth', 'Birthday', 'required');
			$this->form_validation->set_rules('day_death', 'Death day', 'required');
			$this->form_validation->set_rules('sl_country', 'Country', 'required');
			if($this->form_validation->run() === TRUE) {

				if(!empty($_FILES['photo_cover']['name']))
                {
                    $config['upload_path'] = './data_uploads/person/';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['encrypt_name'] = TRUE;

                    $this->load->library('upload', $config);

                    if ( ! $this->upload->do_upload('photo_cover'))
                    {
                        echo $this->upload->display_errors();
                        $image = null;
                    }
                    else
                    {
                        $up = $this->upload->data();
                        $image = 'data_uploads/person/' . $up['file_name'];

                        $imageWidth = $up['image_width'];
                        if($imageWidth > 150)
                        {
                            $this->load->library('image_lib');
                            $config['image_library'] = 'GD2';
                            $config['source_image'] = $up['full_path'];
                            $config['quality'] = 75;
                            $config['maintain_ratio'] = TRUE;
                            $config['master_dim'] = 'auto';
                            $config['width'] = 150;
                            $config['height'] = 150;

                            $this->image_lib->initialize($config);
                            $this->image_lib->resize();
                        }

                    }
                } else {
                    $image = null;
                }  

				$birthday = $this->input->post('day_birth');
				$deathday = $this->input->post('day_death');
				$arr = array(
					'member_id'		=> $this->session->userdata('sess_memId'),
					'name'			=> $this->input->post('name'),
					'day_birth'		=> strtotime($this->input->post('day_birth')),
					'day_death'		=> strtotime($this->input->post('day_death')),
					'country'		=> $this->input->post('sl_country'),
					'state'			=> $this->input->post('sub_region'),
					'long_desc'		=> $this->input->post('desc'),
					'age'			=> $this->util->calculator_age($deathday, $birthday),
					'public'		=> $this->input->post('chk'),
					'photo_cover'	=> $image
				);

				$this->db->insert('person', $arr);
				// $person_id = $this->db->insert_id();
				// if($image !== null) {
				// 	$this->db->insert('photo_person', array('person_id' => $person_id, 'photo' => $image));
				// }

				redirect('my-added');
			}
		}

		$countries = $this->db->select('id, country')->get('regions')->result_array();
		$this->template->title('Death', 'Add a person')
			->set('countries', $countries)
			->build('add_person/index');
	}

	public function get_sub_region() {
		$region_id = (int)$this->input->post('region_id');
		$sql = $this->db->select('id, name')->get_where('subregions', array('region_id' => $region_id))->result_array();

		if(count($sql)) {
			foreach($sql as $row) {
				echo "<option value='".$row['id']."'>".$row['name']."</option>";
			}
		} else {
			echo "<option value=''>Select a state</option>";
		}
	}
}
